﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GDViewComponent.h"

#include "GDDefine.h"
#include "../Util/GDUtil.h"

GDViewComponent::GDViewComponent()
{
}


GDViewComponent::~GDViewComponent()
{
}

bool GDViewComponent::init(){
	if (!CAView::init()){
		return false;
	}

	return true;
}

void GDViewComponent::initView(){
	viewSize = this->getBounds().size;
}

SplitLineViewComponent::SplitLineViewComponent()
: lineView(NULL)
, titleView(NULL)
, titleLabel(NULL)
{

}

SplitLineViewComponent::~SplitLineViewComponent()
{
}

SplitLineViewComponent* SplitLineViewComponent::createWithFrame(const CCRect& rect, std::string title, unsigned long fontSize){

	SplitLineViewComponent *instance = new SplitLineViewComponent();
	if (instance && instance->initWithFrame(rect))
	{
		instance->title = title;
		instance->fontSize = fontSize;
		instance->initView();
		instance->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(instance);
	}
	return instance;
}

SplitLineViewComponent* SplitLineViewComponent::createWithCenter(const CCRect& rect, std::string title, unsigned long fontSize){

	SplitLineViewComponent *instance = new SplitLineViewComponent();
	if (instance && instance->initWithCenter(rect))
	{
		instance->title = title;
		instance->fontSize = fontSize;
		instance->initView();
		instance->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(instance);
	}
	return instance;
}

bool SplitLineViewComponent::init(){
	if (!GDViewComponent::init()){
		return false;
	}

	return true;
}

void SplitLineViewComponent::initView(){
	GDViewComponent::initView();

	lineView = CAView::createWithCenter(CCRect(viewSize.width*0.5, viewSize.height*0.5, viewSize.width, _ds(1)));
	lineView->setColor(ccc4(234, 234, 234, 255)); // 设置线颜色
	this->addSubview(lineView);

	CCRect titleLabelCCRect = CCRect(_ds(5), viewSize.height*0.5, viewSize.width, viewSize.height);
	titleLabel = CALabel::createWithCenter(titleLabelCCRect);
	titleLabel->setText(title);
	titleLabel->setFontSize(fontSize);
	titleLabel->setTextAlignment(CATextAlignmentCenter);
	titleLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	titleLabel->setNumberOfLine(1);
	titleLabel->sizeToFit();
	titleLabel->visit();

	titleLabel->setFrameOrigin(CCPoint(_ds(5), titleLabel->getFrameOrigin().y));

	CCRect titleViewCCRect = CCRect(viewSize.width*0.5, viewSize.height*0.5, titleLabel->getBounds().size.width + _ds(10), viewSize.height);
	titleView = CAView::createWithCenter(titleViewCCRect);
	this->addSubview(titleView);

	titleView->addSubview(titleLabel);
}

void SplitLineViewComponent::setColor(const CAColor4B& color){
	CAView::setColor(color);
	titleView->setColor(color);
}

void SplitLineViewComponent::setTitleColor(const CAColor4B& color){
	if (titleLabel){
		titleLabel->setColor(color);
	}
}

void SplitLineViewComponent::setLineColor(const CAColor4B& color){
	if (lineView){
		lineView->setColor(color);
	}
}


OptionViewCell::OptionViewCell()
:titleLabe(NULL)
{

}

OptionViewCell::~OptionViewCell()
{

}

OptionViewCell* OptionViewCell::create(const std::string& identifier, const CADipRect& _rect)
{
	OptionViewCell* listViewCell = new OptionViewCell();
	if (listViewCell&&listViewCell->initWithReuseIdentifier(identifier))
	{
		listViewCell->setFrame(_rect);
		listViewCell->setColor(CAColor_clear);
		listViewCell->autorelease();
		return listViewCell;
	}
	CC_SAFE_DELETE(listViewCell);
	return NULL;
}

void OptionViewCell::setOption(std::string title, std::string value){
	this->titleLabe->setText(title);
	this->optionValue = title;
}

void OptionViewCell::initWithCell()
{
	CADipSize _size = this->getFrame().size;
	titleLabe = CALabel::createWithCenter(CADipRect(_size.width*0.5,_size.height*0.5,_size.width*0.9,_size.height));
	titleLabe->setTextAlignment(CATextAlignmentCenter);
	titleLabe->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	titleLabe->setFontSize(_ds(30));
	titleLabe->setTag(100);
	this->addSubview(titleLabe);
}

void OptionViewCell::cellBtnCallback(CAControl* btn, CCPoint point)
{

}

void OptionViewCell::normalListViewCell()
{
	if (titleLabe){
		titleLabe->setColor(CAColor_gray);
	}
	this->setBackgroundView(CAView::createWithColor(CAColor_white));
}

void OptionViewCell::highlightedListViewCell()
{
	if (titleLabe){
		titleLabe->setColor(CAColor_gray);
	}
	this->setBackgroundView(CAView::createWithColor(CAColor_white));
}

void OptionViewCell::selectedListViewCell()
{
	if (titleLabe){
		titleLabe->setColor(CAColor_black);
	}
	this->setBackgroundView(CAView::createWithColor(CAColor_white));
}

void OptionViewCell::disabledListViewCell()
{
	if (titleLabe){
		titleLabe->setColor(CAColor_gray);
	}
	this->setBackgroundView(CAView::createWithColor(CAColor_white));
}

SelectPopView::SelectPopView()
:m_SelectViewDelegate(NULL)
, titleLabel(NULL)
, rootNode(NULL)
, listDataNode(NULL)
, listView(NULL)
, curSelectIndex(-1)
,m_itemHeight(60)
{
	valueList.clear();
	titleList.clear();
}

SelectPopView::~SelectPopView(){

}



SelectPopView* SelectPopView::create(CAView *superView, std::string title){
	CCRect rect = CCRect(0, 0, superView->getBounds().size.width, superView->getBounds().size.height);
	SelectPopView *view = new SelectPopView();
	if (view && view->initWithFrame(rect)){
		view->title = title;
		view->initView();
		//view->autorelease();
		//int zorder = GDUtil::getMaxZOrderInSubView(superView);
		//CCLog("%s max zorder：%d", __FUNCTION__, zorder);
		view->setZOrder(999999);
		superView->addSubview(view);

		view->setVisible(false);
		return view;
	}
	return NULL;
}



bool SelectPopView::init(){
	if (!CAView::init()){
		return false;
	}
	m_itemHeight = _ds(60);
	return true;
}

void SelectPopView::onCloseBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point){
	this->setVisible(false);
}

void SelectPopView::initView(){
	GDViewComponent::initView();

	CAButton *closeBtn = CAButton::createWithFrame(CCRect(0, 0, viewSize.width, viewSize.height), CAButtonTypeCustom);
	this->addSubview(closeBtn);
	closeBtn->addTarget(this, CAControl_selector(SelectPopView::onCloseBtnClick), CAControlEventTouchUpInSide);
	
	CCRect rect = CCRect(viewSize.width*0.5, viewSize.height*0.5, viewSize.width, viewSize.height*0.5);

	rootNode = CAView::createWithCenter(rect);
	//rootNode->setColor(CAColor_red);
	this->addSubview(rootNode);

	int height = _ds(54);

	if (title.empty() || title == ""){
		height = _ds(15);
	}
	else{

	}
	CAView *titleBg = CAView::createWithColor(CAColor_white);
	titleBg->setFrame(CCRect(1, 1, rect.size.width - 2, height));
	rootNode->addSubview(titleBg);

	if (title.empty() || title == ""){}else{
		titleLabel = CALabel::createWithFrame(CCRect(0, 0, titleBg->getBounds().size.width, titleBg->getBounds().size.height));
		titleLabel->setFontSize(_ds(30));
		titleLabel->setText(title);
		titleLabel->setColor(CAColor_orange);
		titleLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
		titleLabel->setTextAlignment(CATextAlignmentCenter);
		titleBg->addSubview(titleLabel);
	}

	height = titleBg->getFrameOrigin().y + titleBg->getBounds().size.height;
	listDataNode = CAView::createWithColor(CAColor_white);
	listDataNode->setFrame(CCRect(1, height, rect.size.width - 2, rect.size.height - height - 1));
	rootNode->addSubview(listDataNode);

	listMaxHeight = listDataNode->getBounds().size.height - 15;
	listView = CAListView::createWithFrame(CCRect(0, 0, listDataNode->getBounds().size.width, listMaxHeight));
	listView->setListViewDelegate(this);
	listView->setListViewDataSource(this);
	listView->setAllowsSelection(true);
	listView->setListViewOrientation(CAListViewOrientationVertical);
	//listView->setBackGroundColor(CAColor_white);
	listView->setListHeaderHeight(0);
	listView->setListFooterHeight(0);
	listView->setShowsScrollIndicators(false);
	listDataNode->addSubview(listView);
	
	this->setColor(ccc4(0, 0, 0, 56));
}

void SelectPopView::resetHeight(){
	int height = (m_itemHeight + 1) * this->countOptions();
	if (height > listMaxHeight){
		height = listMaxHeight;
	}
	// CCLog("list高度：%d",height);
	GDUtil::setViewHeight(listView, height);
	GDUtil::setViewHeight(listDataNode, listView,15);
	GDUtil::setViewHeight(rootNode, listDataNode,1);
	//GDUtil::setSubViewToCenter(this, rootNode);
	rootNode->setFrameOrigin(CCPoint(0, this->getBounds().size.height - rootNode->getBounds().size.height));
}

unsigned int SelectPopView::countOptions(){
	if (this->valueList.empty()){
		return 0;
	}

	return (int)this->valueList.size();
}

void SelectPopView::addOption(string title, string value){
	// CCLog("添加Option,标题：%s,值：%s",title.c_str(),value.c_str());
	titleList.push_back(title);
	valueList.push_back(value);
}

void SelectPopView::addOptions(vector<string> titles, vector<string> values){
	if (titles.empty() || values.empty()){
		return;
	}

	int size = (int)titles.size();
	if (size != values.size()){
		return;
	}
	for (int i = 0; i < size; i++){
		addOption(titles.at(i),values.at(i));
	}
	this->freshSelectView();
}

void SelectPopView::clearOption(){
	titleList.clear();
	valueList.clear();
	this->freshSelectView();
}

void SelectPopView::setSelectAtIndex(unsigned int index){
	
	if (titleList.empty() || titleList.size() < index + 1){
		return ;
	}
	this->curSelectIndex = index;
	listView->setSelectAtIndex(index);
	
	// 修改滚动高度
	int listHeight = listView->getBounds().size.height / 2;
	int cellHeight = listViewHeightForIndex(listView,index)+1;

	int totalHeight = cellHeight * numberOfIndex(listView) - listHeight*2; // 滚动最大高度

	int height = cellHeight * index; // 滚动高度

	if (height > listHeight){
		height = height - listHeight;
	}
	if (height > totalHeight){
		height = totalHeight;
	}
	listView->setContentOffset(CCPoint(0, height),false);
}

void SelectPopView::setSelectValue(std::string value){
	int size = (int)valueList.size();
	for (int i = 0; i < size; i++){
		if (value == valueList.at(i)){
			this->setSelectAtIndex(i);
			break;
		}
	}

}

void SelectPopView::freshSelectView(){
	listView->reloadData();
	listView->setContentOffset(CCPoint(0, 0), false);
	resetHeight();
}

int SelectPopView::getCurSelectIndex(){
	return this->curSelectIndex;
}

std::string SelectPopView::getCurSelectTitle(){
	if (curSelectIndex < 0 || valueList.size() <= 0){
		return "";
	}
	return this->titleList.at(curSelectIndex);
}
std::string SelectPopView::getCurSelectValue(){
	if (curSelectIndex < 0 || valueList.size()<=0){
		return "";
	}
	return this->valueList.at(curSelectIndex);
}

unsigned int SelectPopView::numberOfIndex(CAListView *listView){
	return (int)titleList.size();
}
unsigned int SelectPopView::listViewHeightForIndex(CAListView *listView, unsigned int index){
	return m_itemHeight;
}
CAListViewCell* SelectPopView::listViewCellAtIndex(CAListView *listView, const CCSize& cellSize, unsigned int index){
	OptionViewCell* cell = (OptionViewCell*)listView->dequeueReusableCellWithIdentifier("ListViewCell");
	if (!cell)
	{
		cell = OptionViewCell::create("ListViewCell", CCRect(0, 0, cellSize.width, cellSize.height));
		cell->initWithCell();
	}

	cell->setControlStateNormal();
	cell->setOption(titleList.at(index), valueList.at(index));
	
	return cell;
}
void SelectPopView::listViewDidSelectCellAtIndex(CAListView *listView, unsigned int index){
	this->curSelectIndex = index;
	this->setVisible(false);

	if (m_SelectViewDelegate){
		m_SelectViewDelegate->selectOption(this->getSuperview(), index, valueList.at(index), titleList.at(index));
	}
}
void SelectPopView::listViewDidDeselectCellAtIndex(CAListView *listView, unsigned int index){

}

void SelectPopView::setVisible(bool visible){
	if (visible && this->curSelectIndex >= 0){
		setSelectAtIndex(this->curSelectIndex);
	}

	CAView::setVisible(visible);
}

SelectViewComponent::SelectViewComponent()
:m_SelectViewDelegate(NULL)
, selectPopView(NULL)
, selectBtn(NULL)
, titleLabel(NULL)
{
}

SelectViewComponent::~SelectViewComponent(){

}

SelectViewComponent* SelectViewComponent::createWithFrame(CAViewController *controller, CCRect rect, std::string title, std::string popViewTitle){
	SelectViewComponent *view = new SelectViewComponent();
	if (view && view->initWithFrame(rect)){	
		view->initView(controller, title,popViewTitle);

		return view;
	}
	return NULL;
}

SelectViewComponent* SelectViewComponent::createWithCenter(CAViewController *controller, CCRect rect, std::string title, std::string popViewTitle){
	SelectViewComponent *view = new SelectViewComponent();
	if (view && view->initWithCenter(rect)){
		view->initView(controller, title,  popViewTitle);

		return view;
	}
	return NULL;
}

bool SelectViewComponent::init(){
	if (!CAView::init()){
		return false;
	}

	return true;
}

void SelectViewComponent::initView(CAViewController *controller, std::string title, std::string popViewTitle){
	GDViewComponent::initView();

	// 添加选择按钮
	selectBtn = CAButton::createWithFrame(CCRect(0, 0, viewSize.width, viewSize.height), CAButtonTypeCustom);
	//CAScale9ImageView *selectBtnBgImg = CAScale9ImageView::createWithImage(CAImage::create("source_material/select_bg.png"));
	//selectBtn->setBackGroundViewForState(CAControlStateNormal, selectBtnBgImg);
	selectBtn->addTarget(this, CAControl_selector(SelectViewComponent::onSelectBtnClick), CAControlEventTouchUpInSide);
	this->addSubview(selectBtn);

	defaultTitle = title;
	int titleHeight = viewSize.height*0.628;
	titleLabel = CALabel::createWithFrame(CCRect(0, 0, viewSize.width, viewSize.height));
	titleLabel->setFontSize(titleHeight);
	//titleLabel->setTextAlignment(CATextAlignmentCenter);
	titleLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	titleLabel->setNumberOfLine(1);
	titleLabel->setText(title);
	titleLabel->setZOrder(2);
	titleLabel->setColor(GD_COLOR_8b7769);
	selectBtn->addSubview(titleLabel);

	selectPopView = SelectPopView::create(controller->getView(), popViewTitle);
	selectPopView->setSelectViewDelegate(this);
}

void SelectViewComponent::onSelectBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point){
	if (IS_DEBUG) CCLog("%s 打开列表", __FUNCTION__);
	if (!selectPopView->isVisible()){
		if (selectPopView->countOptions() == 0){
			GDUtil::alert("没有数据");
		}
		else{
			tmpSelectIndex = this->selectPopView->getCurSelectIndex();
			selectPopView->setVisible(true);
		}
	}
}

void SelectViewComponent::selectOption(CAView *selectView, unsigned int index, std::string value, std::string title){
	if (tmpSelectIndex != this->selectPopView->getCurSelectIndex()){ // 如果选则了新的项
		titleLabel->setText(title);

		if (m_SelectViewDelegate){
			m_SelectViewDelegate->selectOption(this, index, value, title);
		}
	}
}

unsigned int SelectViewComponent::countOptions(){
	return selectPopView->countOptions();
}

void SelectViewComponent::addOptions(vector<string> titles, vector<string> values){
	selectPopView->addOptions(titles, values);
}

void SelectViewComponent::addOption(string title, string value){
	selectPopView->addOption(title, value);
}

void SelectViewComponent::clearOption(){
	selectPopView->clearOption();
	setTitle(defaultTitle);
}

void SelectViewComponent::freshSelectView(){
	selectPopView->freshSelectView();
}

std::string SelectViewComponent::getSelectTitle(){
	return selectPopView->getCurSelectTitle();
}

std::string SelectViewComponent::getSelectValue(){
	return selectPopView->getCurSelectValue();
}

unsigned int SelectViewComponent::getSelectIndex(){
	return selectPopView->getCurSelectIndex();
}

void SelectViewComponent::setSelectValue(std::string value){
	selectPopView->setSelectValue(value);
	setTitle(selectPopView->getCurSelectTitle());
}

void SelectViewComponent::setSelectAtIndex(unsigned int index){
	selectPopView->setSelectAtIndex(index);
	setTitle( selectPopView->getCurSelectTitle());
}

void SelectViewComponent::setTitle(std::string title){
	titleLabel->setText(title);
}

void SelectViewComponent::setImageForState(const CAControlState& controlState, CAImage* var){
	selectBtn->setImageForState(controlState, var);
}

void SelectViewComponent::setTitleFontSize(unsigned int fontSize){
	this->titleLabel->setFontSize(fontSize);
}

void SelectViewComponent::setTitleColor(const CAColor4B& color){
	this->titleLabel->setColor(color);
}

CALabel *SelectViewComponent::getTitleLabel(){
	return this->titleLabel;
}

GDButton::GDButton(int type)
:btn(NULL)
, titleLabel(NULL)
{
	this->type = type;
}

GDButton::~GDButton(){

}

GDButton* GDButton::createWithCenter(CCRect rect, std::string title, GDButtonType type){
	GDButton *view = new GDButton(type);
	view->initWithColor(GD_COLOR_ffffff);
	view->initView(rect.size);
	view->setCenter(rect);
	view->setTitle(title);
	view->setType();
    view->autorelease();
	return view;
}

GDButton* GDButton::createWithFrame(CCRect rect, std::string title, GDButtonType type){
	GDButton *view = new GDButton(type);
	view->initWithColor(GD_COLOR_ffffff);
	view->initView(rect.size);
	view->setFrame(rect);
	view->setTitle(title);
	view->setType();
    view->autorelease();
	return view;
}

void GDButton::initView(CCSize& size){
	this->setColor(GD_COLOR_dabca4);
	
	btn = CAButton::createWithFrame(CCRect(_ds(2), _ds(2), size.width - _ds(4) , size.height - _ds(4)), CAButtonTypeCustom);
	btn->setBackGroundViewForState(CAControlStateAll, CAView::createWithColor(CAColor_white));
	btn->setBackGroundViewForState(CAControlStateHighlighted, CAView::createWithColor(GD_COLOR_eee9e5));

	titleLabel = CALabel::createWithFrame(btn->getBounds());
	titleLabel->setFontSize(_ds(28));
	titleLabel->setColor(GD_COLOR_cb8553);
	titleLabel->setTextAlignment(CATextAlignmentCenter);
	titleLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	
	btn->addSubview(titleLabel);
	this->addSubview(btn);
}

void GDButton::setType(){
	switch (type){
	case GDButtonType_LOGIN_BTN:{
		this->setColor(GD_COLOR_dabca4);
		btn->setBackGroundViewForState(CAControlStateAll, CAView::createWithColor(CAColor_white));
		btn->setBackGroundViewForState(CAControlStateHighlighted, CAView::createWithColor(GD_COLOR_eee9e5));
		titleLabel->setColor(GD_COLOR_cb8553);
		//titleLabel->setFontName(GD_FONT_Hoefler_Text);
		titleLabel->setFontName("");
		break;
	}
	case GDButtonType_LEFT_BIG_BTN:{
		this->setColor(CAColor_clear);
		btn->setBackGroundViewForState(CAControlStateAll, CAScale9ImageView::createWithImage(CAImage::create(GD_IMAGE_PATH + "left_big_btn.png")));
		titleLabel->setColor(ccc4(CAColor_white.r, CAColor_white.g, CAColor_white.b, 255 * 0.8));
		break;
	}
	case GDButtonType_LEFT_KUANG_BTN:{
		this->setColor(CAColor_clear);
		btn->setBackGroundViewForState(CAControlStateAll, CAScale9ImageView::createWithImage(CAImage::create(GD_IMAGE_PATH + "left_kuang_btn.png")));
		titleLabel->setColor(ccc4(CAColor_white.r, CAColor_white.g, CAColor_white.b, 255 * 0.8));
		break;
	}
	case GDButtonType_GOODS_GRAB_SITE_BTN:{
		this->setColor(GD_COLOR_2eb2c2);
		btn->setFrame(CCRect(_ds(1), _ds(1), this->getBounds().size.width - _ds(2), this->getBounds().size.height - _ds(2)));
		btn->setBackGroundViewForState(CAControlStateAll, CAView::createWithColor(CAColor_white));
		titleLabel->setColor(GD_COLOR_2eb2c2);
		titleLabel->setFontName("");
		titleLabel->setFontSize(_ds(22));
		break;
	}
	case GDButtonType_GOODS_GRAB_POST:{
		this->setColor(GD_COLOR_d9bba1);
		//btn->setFrame(CCRect(_ds(1), _ds(1), this->getBounds().size.width - _ds(2), this->getBounds().size.height - _ds(2)));
		btn->setBackGroundViewForState(CAControlStateAll, CAView::createWithColor(CAColor_white));
		titleLabel->setColor(GD_COLOR_c87d47);
		titleLabel->setFontName("");
		titleLabel->setFontSize(_ds(36));
		break;
	}
	case GDButtonType_GOODS_LIST_FILTER_OK:{
		this->setColor(GD_COLOR_cec0b7);
		btn->setFrame(CCRect(_ds(1), _ds(1), this->getBounds().size.width - _ds(2), this->getBounds().size.height - _ds(2)));
		btn->setBackGroundViewForState(CAControlStateAll, CAView::createWithColor(GD_COLOR_eeeae4));
		titleLabel->setColor(GD_COLOR_97664b);
		titleLabel->setFontSize(_ds(22));
		break;
	}
	}
}

CAButton* GDButton::getBtn(){
	return btn;
}

void GDButton::setTitle(std::string title){
	titleLabel->setText(title);
}

CALabel* GDButton::getTitleLabel(){
	return titleLabel;
}

void GDButton::addTarget(CAObject *target, SEL_CAControl selector, CAControlEvents event){
	btn->addTarget(target, selector, event);
}